﻿Imports System.Text.RegularExpressions

Public Class FormRegEx

    Private Sub AjouterMasque(ByVal Masque As String)
        Me.TextBoxRegEx.SelectedText = Masque
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.ContextMenuRegEx.Show(Me.Button2, New System.Drawing.Point(0, 0))
    End Sub

    Private Sub AlphabétiqueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlphabétiqueToolStripMenuItem.Click
        AjouterMasque("\w")
    End Sub

    Private Sub NonAlphabétiqueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NonAlphabétiqueToolStripMenuItem.Click
        AjouterMasque("\W")
    End Sub

    Private Sub DécimalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DécimalToolStripMenuItem.Click
        AjouterMasque("\d")
    End Sub

    Private Sub NonDécimalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NonDécimalToolStripMenuItem.Click
        AjouterMasque("\D")
    End Sub

    Private Sub EspacementToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EspacementToolStripMenuItem.Click
        AjouterMasque("\s")
    End Sub

    Private Sub NonEspacementToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NonEspacementToolStripMenuItem.Click
        AjouterMasque("\S")
    End Sub

    Private Sub TabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabToolStripMenuItem.Click
        AjouterMasque("\t")
    End Sub

    Private Sub RetourToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RetourToolStripMenuItem.Click
        AjouterMasque("\r")
    End Sub

    Private Sub NouvelleLigneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NouvelleLigneToolStripMenuItem.Click
        AjouterMasque("\n")
    End Sub

    Private Sub BackslachToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackslachToolStripMenuItem.Click
        AjouterMasque("\\")
    End Sub

    Private Sub AucunOuUnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AucunOuUnToolStripMenuItem.Click
        AjouterMasque("?")
    End Sub

    Private Sub AucuneOuPlusieurToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AucuneOuPlusieurToolStripMenuItem.Click
        AjouterMasque("*")
    End Sub

    Private Sub UnOuPlusieursToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnOuPlusieursToolStripMenuItem.Click
        AjouterMasque("+")
    End Sub

    Private Sub AussiPeuQuePossibleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AussiPeuQuePossibleToolStripMenuItem.Click
        AjouterMasque("+?")
    End Sub

    Private Sub AucuneOuAussiPeuQuePossibleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AucuneOuAussiPeuQuePossibleToolStripMenuItem.Click
        AjouterMasque("*?")
    End Sub

    Private Sub PositiveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PositiveToolStripMenuItem.Click
        AjouterMasque("[]")
    End Sub

    Private Sub NégativeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NégativeToolStripMenuItem.Click
        AjouterMasque("[^]")
    End Sub

    Private Sub LettreMajusculeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LettreMajusculeToolStripMenuItem.Click
        AjouterMasque("\p{Lu}")
    End Sub

    Private Sub LettreMinusculeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LettreMinusculeToolStripMenuItem.Click
        AjouterMasque("\p{Ll}")
    End Sub

    Private Sub DébutDeLigneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DébutDeLigneToolStripMenuItem.Click
        AjouterMasque("^")
    End Sub

    Private Sub FinDeLigneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FinDeLigneToolStripMenuItem.Click
        AjouterMasque("$")
    End Sub

    Private Sub DébutDeTexteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DébutDeTexteToolStripMenuItem.Click
        AjouterMasque("\A")
    End Sub

    Private Sub FinDeTexteAvantLigneVideToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FinDeTexteAvantLigneVideToolStripMenuItem.Click
        AjouterMasque("\Z")
    End Sub

    Private Sub FinDeTexteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FinDeTexteToolStripMenuItem.Click
        AjouterMasque("\z")
    End Sub

    Private Sub LimiteEntreDeuxMotsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LimiteEntreDeuxMotsToolStripMenuItem.Click
        AjouterMasque("\b")
    End Sub

    Private Sub QuelconqueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuelconqueToolStripMenuItem.Click
        AjouterMasque(".")
    End Sub

    Private Sub SeulToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeulToolStripMenuItem.Click
        AjouterMasque("{1}")
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        AjouterMasque("{2}")
    End Sub

    Private Sub NOuPlusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NOuPlusToolStripMenuItem.Click
        AjouterMasque("{2,}")
    End Sub

    Private Sub EntreNEtMToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EntreNEtMToolStripMenuItem.Click
        AjouterMasque("{2,4}")
    End Sub

    Private Sub ExpressionToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpressionToolStripMenuItem.Click
        If Me.TextBoxRegEx.SelectionLength = 0 Then
            AjouterMasque("()")
        Else
            Me.TextBoxRegEx.Text = Me.TextBoxRegEx.Text.Insert(Me.TextBoxRegEx.SelectionStart, "(")
            Me.TextBoxRegEx.Text = Me.TextBoxRegEx.Text.Insert(Me.TextBoxRegEx.SelectionStart + Me.TextBoxRegEx.SelectionLength + 1, ")")
        End If
    End Sub

    Private Sub OuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuToolStripMenuItem.Click
        AjouterMasque("(Mot1|Mot2)")
    End Sub

    Private Sub QuelconqueToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuelconqueToolStripMenuItem1.Click
        AjouterMasque("\b\d+(?:[\.,]\d+)?\b")
    End Sub

    Private Sub EntierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EntierToolStripMenuItem.Click
        AjouterMasque("\b[^\.\D]+\b")
    End Sub

    Private Sub RéelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RéelToolStripMenuItem.Click
        AjouterMasque("\b\d+[\.,]\d+\b")
    End Sub

    Private Sub HexadécimalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HexadécimalToolStripMenuItem.Click
        AjouterMasque("\b[0-9a-fA-F]+\b")
    End Sub

    Private Sub DateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateToolStripMenuItem.Click
        AjouterMasque("(\d{2}|\d{4})-(\d{2})-(\d{2}|\d{4})")
    End Sub

    Private Sub HeureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HeureToolStripMenuItem.Click
        AjouterMasque("\d\d:\d\d(:\d\d)?")
    End Sub

    Private Sub MotToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MotToolStripMenuItem.Click
        AjouterMasque("\b\w+\b")
    End Sub

    Private Sub ChaineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChaineToolStripMenuItem.Click
        AjouterMasque("""(.+)""")
    End Sub

    Private Sub LigneVideToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LigneVideToolStripMenuItem.Click
        AjouterMasque("^\s*$")
    End Sub

    Private Sub ChaineAlphaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChaineAlphaToolStripMenuItem.Click
        AjouterMasque("\w+")
    End Sub

    Private Sub NomDeFichierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NomDeFichierToolStripMenuItem.Click
        AjouterMasque("(([a-z]:)|(\\\\[^<>:""/\\\?\*\.\r\n]+(\\[^<>:""/\\\?\*\.\r\n]+)))(\\[^<>:""/\\\?\*\.\r\n]+)+(\.[^<>:""/\\\?\*\.\r\n]+)+")
    End Sub

    Private Sub NimporteQuoiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NimporteQuoiToolStripMenuItem.Click
        AjouterMasque(".*")
    End Sub

    Private Sub QuelquechoseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuelquechoseToolStripMenuItem.Click
        AjouterMasque(".+")
    End Sub

    Private Sub EscpacessToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EscpacessToolStripMenuItem.Click
        AjouterMasque("\s+")
    End Sub

    Private Sub LimiteDeMotToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LimiteDeMotToolStripMenuItem.Click
        AjouterMasque("\b")
    End Sub

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        FormBrowser.WebBrowser1.DocumentText = My.Resources.RegExHelp
        FormBrowser.Show()
    End Sub

    Private Sub ButtonTester_Click(sender As System.Object, e As System.EventArgs) Handles ButtonTester.Click
        Call Me.Tester()
    End Sub

    Private Sub Tester()
        Dim Options As RegexOptions = RegexOptions.Multiline Or RegexOptions.IgnoreCase
        If Me.CheckBoxMulti.Checked Then Options = Options Or RegexOptions.Singleline
        Me.DataGridView1.Rows.Clear()
        Me.DataGridView1.Columns.Clear()
        Me.RichTextBoxTest.SelectAll()
        Me.RichTextBoxTest.SelectionBackColor = Color.White
        Me.RichTextBoxTest.SelectionColor = Color.Black
        Dim oColMatch As MatchCollection
        Try
            oColMatch = System.Text.RegularExpressions.Regex.Matches(Me.RichTextBoxTest.Text, Me.TextBoxRegEx.Text, Options)
        Catch ex As Exception
            MsgBox("Expression régulière invalide " & Me.TextBoxRegEx.Text, MsgBoxStyle.Exclamation)
            Exit Sub
        End Try
        'Il faut ajouter les colonnes avant les lignes
        If oColMatch.Count > 0 Then
            For i = 0 To oColMatch.Item(0).Groups.Count - 1
                Me.DataGridView1.Columns.Add("Col" & i.ToString, "$" & i.ToString)
            Next
        End If
        Dim IRowIndex As Integer
        For Each oMatch As Match In oColMatch
            IRowIndex = Me.DataGridView1.Rows.Add()
            Dim coulMatch As System.Drawing.Color = Color.Pink
            For iIndex As Integer = 0 To oMatch.Groups.Count - 1
                Me.DataGridView1.Rows(IRowIndex).Cells(iIndex).Value = oMatch.Groups(iIndex).Value
                RichTextBoxTest.Select(oMatch.Groups(iIndex).Index, oMatch.Groups(iIndex).Length)
                If iIndex = 0 Then
                    RichTextBoxTest.SelectionBackColor = coulMatch
                Else
                    If iIndex Mod 2 = 0 Then
                        RichTextBoxTest.SelectionBackColor = ControlPaint.Light(coulMatch)
                    Else
                        RichTextBoxTest.SelectionBackColor = ControlPaint.LightLight(coulMatch)
                    End If
                End If
            Next
        Next

    End Sub

    Public Function AutoRegex(ByVal Chaine As String) As String
        'Étape 1 - Remplacer les caractères litéraux par des \s, \w et \d
        Dim Sortie As String = ""
        Dim car As String
        Chaine = EchapRegex(Chaine)
        For i As Integer = 0 To Chaine.Length - 1
            car = Chaine.Substring(i, 1)
            If TestRegex(car, "\d") Then
                car = "\d"
            ElseIf TestRegex(car, "\w") Then
                car = "\w"
            ElseIf TestRegex(car, "\s") Then
                car = "\s"
            End If
            Sortie &= car
        Next
        'Étape 2 - Remplacer les séries de \s, \w et \d par des +
        Sortie = System.Text.RegularExpressions.Regex.Replace(Sortie, "(\\s){2,}", "\s+")
        Sortie = System.Text.RegularExpressions.Regex.Replace(Sortie, "(\\d){5,}", "\d+")
        Sortie = System.Text.RegularExpressions.Regex.Replace(Sortie, "(\\w){5,}", "\w+")
        Return Sortie
    End Function

    Public Function EchapRegex(ByVal Chaine As String) As String
        Return System.Text.RegularExpressions.Regex.Escape(Chaine)
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBoxRegEx.SelectionLength = 0 Then TextBoxRegEx.SelectAll()
        Me.TextBoxRegEx.SelectedText = "(" & Me.AutoRegex(Me.TextBoxRegEx.SelectedText) & ")"
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If TextBoxRegEx.SelectionLength = 0 Then TextBoxRegEx.SelectAll()
        Me.TextBoxRegEx.SelectedText = Me.EchapRegex(Me.TextBoxRegEx.SelectedText)
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Call Grouper()
    End Sub

    Private Sub Grouper()
        If TextBoxRegEx.SelectionLength = 0 Then TextBoxRegEx.SelectAll()
        Dim iStart As Integer = TextBoxRegEx.SelectionStart
        Dim iLength As Integer = TextBoxRegEx.SelectionLength
        Me.TextBoxRegEx.SelectedText = "(" & Me.TextBoxRegEx.SelectedText & ")"
        Me.TextBoxRegEx.Focus()
        Me.TextBoxRegEx.SelectionStart = iStart + 1
        Me.TextBoxRegEx.SelectionLength = iLength
    End Sub

    Private Sub ButtonOk_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOk.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub ButtonCancel_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub FormRegEx_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        Me.DataGridView1.Rows.Clear()
        Me.DataGridView1.Columns.Clear()
    End Sub
End Class